

class VisualEditor < Controller

  def index e
    <<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <script>
    visual_editor_inner_loaded_called = false;
    visual_editor_inner_loaded = function() {
      visual_editor_inner_loaded_called = true;
    }
    visual_editor_css_rules = #{CssManager.instance.to_json}
  </script>
  <script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>

  <script type="text/javascript" src="/bublifuk/js/base64.js"></script>
  <script type="text/javascript" src="/bublifuk/js/css_editor.js"></script>
  <script type="text/javascript" src="/bublifuk/js/visual_editor.js"></script>

  <!-- good ol' yui 2 -->
  <link rel="stylesheet" type="text/css"
    href="http://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/treeview/assets/skins/sam/treeview.css">
  <!-- Individual YUI JS files -->
  <script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js">
  </script>
  <script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/treeview/treeview-min.js">
  </script>

  <link rel="stylesheet" href="/bublifuk/visual_editor.css" type="text/css" media="screen" />

</head>
<body>
  <script>launch_visual_editor()</script>
  <iframe src="#{e.sublink :show}" id="page">

</body>
</html>
HTML
  end

  def show e    
    return VisualEditorShow.go e
  end

  def edit e
    target, record = get_target(e.params['p'])
    target.creator_form(e).to_s
  end

  def save e
    #p e.params
    require 'base64'
    begin
      target, record = get_target(e.params['form_unique_path'])


      #e.data['page'] = record

      r = {
        :form => target.creator_form(e).to_s,
        :change => target.render(e).to_s,
      }.to_json

      record.save
    rescue Exception => ex
      r = {
        :exception => Rack::ShowExceptions.new(self).pretty(e.env, ex)[0]
      }.to_json
    end
    e.set_response(Base64.encode64(r), 200, {'Content-Type' => 'text/plain'})
  end


  def get_target unique_path
    path = unique_path.gsub('__', '::').split('_')

    record = path.shift.split('-')
    record.shift
    thing_class, id = record

    record = Thing.to_const(thing_class).get(id)

    target = record

    while !path.empty?
      info = path.shift.split('-')
      target = target.children[info[1].to_i]
    end
    
    [target, record]
  end


  def event e
    event = JSON.parse(e.params['json'])
    event = Event.new('client:'+event['name'], event['params'])
    p event
    event.fire #collect response, send back
    "OK"
  end

end


class VisualEditorShow < Controller
  def index e

    e.data['on'] ||= {}
    e.data['on']['thing_render'] = lambda { |thing|
      thing.widget.add_primary_class('Thing')
      thing.widget.add_class(thing.unique_path);
    }

    node = Tree.instance.destination(e)

    if !node
      node = NodeThing.new
      node.is_root = true
      node.title = 'ROOT'
      node.save
    end    

    node.respond(e) {
      add_spice e.page, e
    }
  end

  def add_spice page, e
    #page.head << '<script type="text/javascript" src="/js/visual_editor.js"></script>'
    page.head << '<link rel="stylesheet" href="/bublifuk/visual_editor.css" type="text/css" media="screen" /> '
#    page.head << '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>'
#    page.head << '<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script>'

   page.body << '<script>parent.visual_editor_inner_loaded()</script>'
  end

end